package com.iukcy.hight.listenner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.*;
import com.alibaba.fastjson.JSON;
import com.iukcy.hight.model.TClass;
import com.iukcy.hight.service.ClassService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description: 课程导入导出监听类
 * @ProjectName cloud
 * @Author zhangzhufu
 * @date 2019/12/5 15:05
 */
@Slf4j
public class TClassDataListener extends AnalysisEventListener<TClass> {

    private ClassService classService;

    public TClassDataListener(ClassService classService) {
        this.classService = classService;
    }

    /*
     * 批量写入数据库
     */
    private List<TClass> batchList = new CopyOnWriteArrayList<>();
    //批量大小
    private static final int BATCH_SIZE = 3000;
    //默认数据集合为空
    private boolean isNull = true;

    /**
     * @Description 获取头信息
     * @Param [headMap, context]
     * @Return void
     * @Author zhangzhufu
     * @Date 2019/12/5 15:11
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        log.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
    }

    /**
     * @Description 解析到一条数据
     * @Param [vehicle, analysisContext]
     * @Return void
     * @Author zhangzhufu
     * @Date 2019/12/5 15:11
     */
    @Override
    public void invoke(TClass data, AnalysisContext analysisContext) {
        //未解析到数据，excel为空
        if (isNull) isNull = false;
        batchList.add(data);
        if (BATCH_SIZE < batchList.size()) {
            classService.batchAdd(batchList);
            batchList.clear();
        }
    }

    /**
     * @Description 所有数据解析完成
     * @Param [analysisContext]
     * @Return void
     * @Author zhangzhufu
     * @Date 2019/12/5 15:12
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (isNull) throw new RuntimeException("导入数据为空");
        classService.batchAdd(batchList);
        log.info("所有数据解析完成！");
    }

    /**
     * @Description 异常监听[异常停止]
     * @Param [exception, context]
     * @Return void
     * @Author zhangzhufu
     * @Date 2019/12/5 15:12
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        log.error("解析失败，停止执行:{}", exception.getMessage());
        if (exception instanceof ExcelDataConvertException) {
            ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
            throw new Exception(String.format("第%s行，第%s列数据转化异常", excelDataConvertException.getRowIndex() + 1, excelDataConvertException.getColumnIndex() + 1));
        } else if (exception instanceof ExcelAnalysisException) {
            ExcelAnalysisException excelAnalysisException = (ExcelAnalysisException) exception;
            throw new Exception(String.format("数据解析异常，%s", excelAnalysisException.getMessage()));
        } else if (exception instanceof ExcelAnalysisStopException) {
            ExcelAnalysisStopException excelAnalysisStopException = (ExcelAnalysisStopException) exception;
            throw new Exception(String.format("数据解析停止异常，%s", excelAnalysisStopException.getMessage()));
        } else if (exception instanceof ExcelCommonException) {
            ExcelCommonException excelCommonException = (ExcelCommonException) exception;
            throw new Exception(String.format("数据列异常，%s", excelCommonException.getMessage()));
        } else if (exception instanceof ExcelGenerateException) {
            ExcelGenerateException excelGenerateException = (ExcelGenerateException) exception;
            throw new Exception(String.format("数据异常，%s", excelGenerateException.getMessage()));
        } else if (exception instanceof RuntimeException) {
            throw new Exception(exception.getMessage());
        } else {
            throw new Exception("数据导入异常导致失败");
        }
    }
}
